package com.roads.project.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.roads.common.utils.DateUtils;
import com.roads.common.utils.SecurityUtils;
import com.roads.system.domain.SysRoleReport;
import com.roads.system.domain.SysUserReport;
import com.roads.system.mapper.SysRoleReportMapper;
import com.roads.system.mapper.SysUserReportMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.roads.project.mapper.ProReportMapper;
import com.roads.project.domain.ProReport;
import com.roads.project.service.IProReportService;

/**
 * 项目报Service业务层处理
 * 
 * @author nhp
 * @date 2022-06-08
 */
@Service
public class ProReportServiceImpl implements IProReportService {

    @Autowired
    private ProReportMapper mapper;

    @Autowired
    private SysRoleReportMapper roleReportMapper;

    @Autowired
    private SysUserReportMapper userReportMapper;

    /**
     * 根据主键ID查询详情
     * 
     * @param id 项目报主键
     * @return 项目报
     */
    @Override
    public ProReport getModelById(Integer id) {
        return mapper.getModelById(id);
    }

    /**
     * 查询列表
     * 
     * @param model 项目报
     * @return 项目报
     */
    @Override
    public List<ProReport> selectModelList(ProReport model) {
        return mapper.selectModelList(model);
    }

    /**
     * 报表列表
     *
     * @param projectId 项目ID
     * @param userId 用户ID
     * @param reportType 报表类型
     * @return 报表集合
     */
    @Override
    public List<ProReport> selectReportList(Integer projectId, Long userId, String reportType) {
        ProReport search = new ProReport();
        search.setProjectId(projectId);
        search.setUserId(userId);
        search.setReportType(reportType);
        return mapper.selectModelListByUser(search);
    }

    /**
     * 查询已分配角色报表列表
     *
     * @param model 项目报表
     * @return 项目报表集合
     */
    @Override
    public List<ProReport> allocatedRoleReportList(ProReport model) {
        return mapper.allocatedRoleReportList(model);
    }

    /**
     * 查询已分配用户报表列表
     *
     * @param model 项目报表
     * @return 项目报表集合
     */
    @Override
    public List<ProReport> allocatedUserReportList(ProReport model) {
        return mapper.allocatedUserReportList(model);
    }

    /**
     * 查询未分配角色报表列表
     *
     * @param model 项目报表
     * @return 项目报表集合
     */
    @Override
    public List<ProReport> unallocatedRoleReportList(ProReport model) {
        return mapper.unallocatedRoleReportList(model);
    }

    /**
     * 查询未分配用户报表列表
     *
     * @param model 项目报表
     * @return 项目报表集合
     */
    @Override
    public List<ProReport> unallocatedUserReportList(ProReport model) {
        return mapper.unallocatedUserReportList(model);
    }

    /**
     * 是否被引用
     *
     * @param reportIds 报表ID集合
     * @return 结果
     */
    @Override
    public boolean isQuote(Integer[] reportIds) {
        int count = mapper.getCountQuote(reportIds);
        if (count > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 批量选择授权报表角色
     *
     * @param roleId  角色ID
     * @param reportIds 需要授权的报表ID
     * @return 结果
     */
    @Override
    public int authRoleReport(Long roleId, Integer[] reportIds) {
        // 新增角色与报表关联
        List<SysRoleReport> list = new ArrayList<>();
        for (Integer reportId : reportIds) {
            SysRoleReport rr = new SysRoleReport();
            rr.setRoleId(roleId);
            rr.setReportId(reportId);
            list.add(rr);
        }
        return roleReportMapper.batchInsert(list);
    }

    /**
     * 用户批量选择授权报表
     *
     * @param userId  用户ID
     * @param reportIds 需要授权的报表ID
     * @return 结果
     */
    @Override
    public int authUserReport(Long userId, Integer[] reportIds) {
        // 新增用户与报表关联
        List<SysUserReport> list = new ArrayList<>();
        for (Integer reportId : reportIds) {
            SysUserReport ur = new SysUserReport();
            ur.setUserId(userId);
            ur.setReportId(reportId);
            list.add(ur);
        }
        return userReportMapper.batchInsert(list);
    }

    /**
     * 取消授权角色报表
     *
     * @param roleReport 角色与报表关联信息
     * @return 结果
     */
    @Override
    public int deleteAuthRoleReport(SysRoleReport roleReport) {
        return roleReportMapper.deleteRoleReportInfo(roleReport);
    }

    /**
     * 取消授权用户报表
     *
     * @param userReport 用户与报表关联信息
     * @return 结果
     */
    @Override
    public int deleteAuthUserReport(SysUserReport userReport) {
        return userReportMapper.deleteUserReportInfo(userReport);
    }

    /**
     * 批量取消授权角色报表
     *
     * @param roleId  角色ID
     * @param reportIds 需要取消授权的报表数据ID
     * @return 结果
     */
    @Override
    public int deleteAuthRoleReports(Long roleId, Integer[] reportIds) {
        return roleReportMapper.deleteRoleReportInfos(roleId, reportIds);
    }

    /**
     * 批量取消授权用户报表
     *
     * @param userId  用户ID
     * @param reportIds 需要取消授权的报表数据ID
     * @return 结果
     */
    @Override
    public int deleteAuthUserReports(Long userId, Integer[] reportIds) {
        return userReportMapper.deleteUserReportInfos(userId, reportIds);
    }

    /**
     * 新增
     * 
     * @param model 项目报
     * @return 结果
     */
    @Override
    public int insert(ProReport model) {
        model.setCreateBy(SecurityUtils.getUsername());
        model.setCreateTime(DateUtils.getNowDate());
        return mapper.insert(model);
    }

    /**
     * 修改
     * 
     * @param model 项目报
     * @return 结果
     */
    @Override
    public int update(ProReport model) {
        model.setUpdateBy(SecurityUtils.getUsername());
        model.setUpdateTime(DateUtils.getNowDate());
        return mapper.update(model);
    }

    /**
     * 批量删除
     * 
     * @param ids 主键集合
     * @return 结果
     */
    @Override
    public int deleteByIds(Integer[] ids) {
        return mapper.deleteByIds(ids);
    }

    /**
     * 删除
     * 
     * @param id 项目报主键
     * @return 结果
     */
    @Override
    public int delete(Integer id) {
        return mapper.delete(id);
    }
}
